package copyengine.ui.dragdrop.impl.source
{
	import copyengine.ui.dragdrop.IDragDropEngine;
	import copyengine.ui.dragdrop.IDragDropSource;
	import copyengine.ui.dragdrop.IDragDropTarget;
	import copyengine.utils.GeneralUtils;

	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;

	public class CEDragDropSourceCore implements IDragDropSource
	{

		/**
		 * hold  reference of dragdorpEngine.
		 *
		 * WARNINIG::
		 *
		 * source class should only call
		 * dragDropEngine.terminateDragDrop or dragDropEngine.endDragDrop();
		 */
		protected var dragDropEngine:IDragDropEngine;

		protected var dragDropIconContainer:Sprite;


		public function CEDragDropSourceCore()
		{
		}

		public final function set engine(_engine:IDragDropEngine):void
		{
			dragDropEngine = _engine;
		}

		public final function createDragIcon():DisplayObjectContainer
		{
			dragDropIconContainer = new Sprite();
			initializeDragDropIcon();
			return dragDropIconContainer;
		}

		public final function onDragDropEnd():void
		{
			doOnDragDropEnd();
			GeneralUtils.clearChild(dragDropIconContainer);
			dragDropIconContainer = null;
			dragDropEngine = null;
		}

		public final function onDragDropCancel():void
		{
			doOnDragDropCancel();
			dragDropEngine.endDragDrop();
		}

		//========================
		//=== Override able Function
		//========================

		public function onDragDropBegin(_target:IDragDropTarget, _x:Number, _y:Number):void
		{
		}

		public function onEnterTarget(_target:IDragDropTarget):void
		{
		}

		public function onLeaveTarget(_target:IDragDropTarget):void
		{
		}

		public function onMove(_target:IDragDropTarget, _x:Number, _y:Number):void
		{
		}

		public function onDrop(_target:IDragDropTarget, _x:Number, _y:Number):void
		{
		}

		public function onDropConfim(_target:IDragDropTarget, _isAccepted:Boolean):void
		{
			//默认状态下,如果Target没有接受Source,则Source也执行DragDropCancel逻辑
			if (!_isAccepted)
			{
				doOnDragDropCancel();
			}
		}

		protected function initializeDragDropIcon():void
		{
		}

		protected function doOnDragDropCancel():void
		{
		}

		protected function doOnDragDropEnd():void
		{
		}
	}
}